//声明: 该文件需要 swiper,jqueryui,common,toast,jquery

require(["config"], function(config) {
	require(["jquery.ui", "jquery", "sw", "toast", "my.hover"], function(jqui, jq, sw, t, mh) {
		$.get("header.html",function(data){
			$(".top_aa").append(data);
			$("#banner_left_bottom").css({"display":"none"});
			
					//菜单
					
		function creatCategory(data) {
			
			for(var i in data) {
				$('#banner_left_bottom').append("<div class='navleft'><a>" + data[i].aaa_1 + data[i].aaa_2 + "").append("<div class='nav-layer'>");
				for(var j in data[i].aaa_3) {
//									console.log(data[i].aaa_3[j])
					$('.nav-layer').last().append("<div class='ft'><dt><a>" + data[i].aaa_3[j].aaa_4_a + "");
					for(var k in data[i].aaa_3[j].aaa_4_b) {
						$('.ft').last().append("<dd><a>" + data[i].aaa_3[j].aaa_4_b[k] + "")
					}
				}
			}

			var navleft = document.getElementsByClassName("navleft");
			var navlayer = document.getElementsByClassName("nav-layer");
			for(let i = 0; i < navleft.length; i++) {
				navleft[i].onmouseover = function() {
					
					

					for(let j = 0; j < navleft.length; j++) {
						navleft[j].style.background = "#fff";
						navlayer[j].style.display = "none";
					}
					this.style.background = "#F5F5F5";
					navlayer[i].style.display = "block";
					$("#banner_left_bottom").css({"display":"block"})

					navlayer[i].onmouseleave = function() {
						navlayer[i].style.display = "none";
						navleft[i].style.background = "#fff";
						$("#banner_left_bottom").css({"display":"none"})
					}
					navlayer[i].onmouseover = function() {
						navlayer[i].style.display = "block";
						navleft[i].style.background = "#f5f5f5";
						$("#banner_left_bottom").css({"display":"block"})
					}
					navleft[i].onmouseleave = function() {
						navlayer[i].style.display = "none";
						navleft[i].style.background = "#fff";
						
					}
				}

			}
			$("#banner_left").css({"z-index":2,"height":"auto"})
			$("#banner_left_top").mouseover(function(){
				$("#banner_left_bottom").css({"display":"block"})
				
				$("#banner_left_top").mouseleave(function(){
					$("#banner_left_bottom").css({"display":"none"})
				})
			})
		}
		$.get("json1/banner.json", function(data) {
			creatCategory(data);
		})

		});
		
		var smalltu=document.getElementsByClassName("pic1a");
		
		
		for(let a=0;a<smalltu.length;a++){
			smalltu[a].onmouseover=function(){

				$(".pic2a").attr("src","img/bigtu"+(a+1)+".jpg")
				$(".pic3a").attr("src","img/middletu"+(a+1)+".jpg")
			}
		}
		
		
		 var fdj = document.getElementById("fdj");  // 获得最大的盒子
    var small = fdj.children[0];  
    var big = document.getElementById("big1") 
    var mask = small.children[1];  // 小的黄色盒子
   var bigImage = big.children[0];
   	
  
    small.onmouseover = function() {   // 鼠标经过显示出他们
        mask.style.display = "block";
        big.style.display = "block";
    }
    small.onmouseout = function() {
        mask.style.display = "none";
        big.style.display = "none";
    }
    //  鼠标在small 内移动
   var x = 0;
   var y = 0;
    small.onmousemove = function(event) {
        var event = event || window.event;
         x = event.clientX - this.offsetParent.offsetLeft-90- mask.offsetWidth /2;  // 再某个盒子内的坐标
         //alert(this.offsetLeft);
         y = event.clientY - this.offsetParent.offsetTop-this.offsetParent.offsetParent.offsetTop+document.body.scrollTop - mask.offsetHeight /2;
        
         if(x < 0)
         {
             x = 0;
         }
         else if(x > small.offsetWidth - mask.offsetWidth)
         {
             x = small.offsetWidth - mask.offsetWidth;
         }
         if(y<0)
         {
            y = 0;
         }
         else if(y > small.offsetHeight - mask.offsetHeight)
         {
             y = small.offsetHeight - mask.offsetHeight;
         }
         mask.style.left = x + "px";
         mask.style.top = y + "px";
    
         bigImage.style.left =  -x *  big.offsetWidth /small.offsetWidth + "px";
         bigImage.style.top =  -y *  big.offsetHeight /small.offsetHeight + "px";

    }
		



	
		$(".buyAdd").click(function(){
				console.log($("#buyNum").val());
			var a2=Number($("#buyNum").val())+1;
			$("#buyNum").val(a2);
		})
		
		$(".buyReduce").click(function(){
				console.log($("#buyNum").val());
				if($("#buyNum").val()>1){
					var a3=Number($("#buyNum").val())-1;
					$("#buyNum").val(a3);
				}
			
		})


//购物车购物车购物车购物车购物车购物车购物车
				var goods=document.getElementById("addCart");
				var carList = [];

				// 先获取当前cookie
				var cookies = document.cookie.split('; ');
				
				for(var i = 0; i < cookies.length; i++) {
					var arr = cookies[i].split('=');
					if(arr[0] === 'carlist') {
						carList = JSON.parse(arr[1]);
						
					}
					
				}
				
			
				goods.onclick = function() {
					
					

					// 添加到购物车
						
//						console.log()

						// 获取当前li
						var currentLi=document.getElementsByClassName("shuju")[0];
						
						var currentGUID = currentLi.getAttribute('data-guid');

						// 先创建一个对象保存当前商品信息
						var goodsObj = {};
						goodsObj.guid = currentGUID;
						goodsObj.qty = 1;
						goodsObj.name = document.getElementsByClassName("mid_top1")[0].innerText;
						goodsObj.price = document.getElementsByClassName("jiage1")[0].children[1].innerHTML
						goodsObj.imgUrl = "img/nv_lists1.jpg";

						// 如果cookie为空，则直接添加
						if(carList.length === 0) {
							// 添加到carList
							carList.push(goodsObj);
						} else {
							// 先判断cookie中有无相同的guid商品
							for(var i = 0; i < carList.length; i++) {
								// 如果商品已经存在cookie中，则数量+1
								if(carList[i].guid === currentGUID) {
									carList[i].qty++;
									break;
								}
							}
							$(".bba").html(carList[i].qty);
							// 如果原cookie中没有当前商品
							if(i === carList.length) {
								// 添加到carList
								carList.push(goodsObj);
							}

						}
						// 存入cookie
						// 把对象/数组转换诚json字符串：JSON.stringify()
						document.cookie = 'carlist=' + JSON.stringify(carList);
					}

			
		




		$.get("footer.html",function(data){
			$("body").last().append(data);
		})

	});

})